package com.gmf.sugar.ui;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CGrid;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.common.layout.FullLockConflictResolver;

public class Workspace extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3518514309928802733L;
	public static final ApplicationContext CTX;
	
	static {
		CTX = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
	}

	public Workspace(Class<?> clazz) {
		setBounds(40, 40, 800, 600);
		setTitle("Sugar");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		CControl control = new CControl(this);
		add(control.getContentArea());
		control.putProperty(CControl.RESIZE_LOCK_CONFLICT_RESOLVER,
				new FullLockConflictResolver());
		setLayout(new GridLayout(1, 1));
		DefaultSingleCDockable red = create("Red", Color.RED);
		DefaultSingleCDockable green = create("Green", Color.GREEN);
		DefaultSingleCDockable blue = create("Blue", Color.BLUE);

		DefaultSingleCDockable gray1 = create("gray1", Color.GRAY);
		DefaultSingleCDockable gray2 = create("gray2", Color.GRAY);

		control.addDockable(red);
		control.addDockable(green);
		control.addDockable(blue);
		control.addDockable(gray1);
		control.addDockable(gray2);

		CGrid grid = new CGrid();

		grid.add(0, 0, 1, 1, red);
		grid.add(1, 0, 1, 1, green);
		grid.add(2, 0, 1, 1, blue);

		grid.add(3, 0, 1, 1, gray1);
		grid.add(1, 1, 3, 1, gray2);

		control.getContentArea().deploy(grid);

		freeze(red);
		freeze(blue);
		freeze(green);
		setLocationRelativeTo(null);
		setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); 
		setVisible(false);
	}

	static void freeze(DefaultSingleCDockable dockable) {
		dockable.setCloseable(false);
		dockable.setMaximizable(false);
		dockable.setMinimizable(false);
		dockable.setResizeLocked(true);
	}

	static DefaultSingleCDockable create(String title, Color color) {

		JPanel panel = new JPanel();
		panel.setOpaque(true);
		panel.setBackground(color);

		DefaultSingleCDockable dockable = new DefaultSingleCDockable(title,
				title, panel);

		return dockable;

	}
}